/**  
* @Title: UuidUtils.java  
* @Package com.chuan.base.util  
* @Description: TODO(生成全局唯一ID)  
* @author Mr.zhao  
* @date 2018年6月1日  
* @version V1.0  
*/ 
package com.chuan.base.util;

import java.util.UUID;

/**
 * @author Mr.zhao
 * @date 2018年6月1日
 *
 */
public class UuidUtil {
	
	/**
	 * 生成全局唯一id
	 * @param baseSize  生成指定长度的Uuid
	 * @return
	 */
	public synchronized static String createUuid(Integer baseSize) {
		UUID uid = UUID.randomUUID();  
        long idx = uid.getLeastSignificantBits();  
        StringBuilder buff = new StringBuilder();  
        for (int i = 0; i < 12; i++) {  
            buff.append(toa(0x1F & idx));  
            idx >>>= 5;  
        }  
        long idxb = uid.getMostSignificantBits();  
        buff.append(toa((idxb & 0x1) == 0 ? idx : idx + 0x10));  
        idx = (idxb >>> 1);  
        for (int i = 0; i < 13; i++) {  
            buff.append(toa(0x1F & idx));  
            idx >>>= 5;  
        }  
        return buff.reverse().toString().substring(0, baseSize);  
	}
	
	 /** 
     * 生成可见字符 
     *  
     * @param lidx 
     * @return 
     */  
    private static char toa(long lidx) {  
        if (lidx < 10) {  
            return (char) (0x30 + lidx);  
        }  
        lidx += (0x41 - 10);  
        int[] skips = { 0x49, 0x4f, 0x57, 0x5a };  
        for (int ch : skips) {  
            if (lidx < ch) {  
                break;  
            }  
            lidx++;  
        }  
        return (char) lidx;  
    }
}
